package com.example.demo;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MaxSlidingWindow {

    @Test
    public void test() {
        int[] array = {1, 3, -1, -3, 5, 3, 6, 7};
        System.out.println(maxSlidingWindow(array, 3));
    }

    public static int[] maxSlidingWindow(int[] nums, int k) {
        int[] ans = new int[nums.length - k + 1];

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            list.add(nums[i]);
        }

        ans[0] = Collections.max(list);
        int idx = 1;
        for (int i = 1; i <= nums.length - k; i++) {
            list.remove(0);
            list.add(nums[i + k - 1]);
            ans[idx++] = Collections.max(list);
        }

        for (int an : ans) {
            System.out.println(an);
        }
        return ans;
    }
}
